package com.test.service.Impl;

import com.test.dao.UserMapper;
import com.test.domain.User;
import com.test.domain.UserExample;
import com.test.enums.Page;
import com.test.enums.Replay;
import com.test.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * UserServiceImpl
 *
 * @author HY
 * 2021/05/17 8:28
 **/

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public Replay insert(User user) {

        Replay replay = new Replay();

        return null;
    }

    @Override
    public Replay del(int id) {

        Replay replay = new Replay();

        return null;
    }

    @Override
    public Replay update(User user) {

        Replay replay = new Replay();
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        //修改条件
        criteria.andUseridEqualTo(user.getUserid());

        int rs = userMapper.updateByExampleSelective(user,userExample);


        replay.setCode(rs >0?0:-1);
        replay.setMsg(rs > 0?"修改成功":"修改失败");

        return null;
    }

    @Override
    public Replay select(User user, Page page) {

        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();

        //添加条件：学号
        if (user.getNumber() != null && !user.getNumber().equals("")) {
            criteria.andNumberLike("%"+user.getNumber()+"%");
        }
//        姓名
        if (user.getName() != null && !user.getName().equals("")){
            criteria.andNameLike("%"+user.getName()+"%");
        }
        List<User> users = userMapper.selectByExample(userExample);

        userExample.setPage(page.getPage());
        userExample.setLimit(page.getLimit());

        Replay replay = new Replay();
        replay.setCode(0);
        replay.setMsg("查询成功！");
        replay.setData(users);

        return replay;
    }
}
